/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.entity.Interviews;
import bean.entity.Vacances;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author server
 */
@Stateless
@LocalBean
public class InterviewSessionBean {

    @PersistenceContext(unitName = "RecruitmentProcessSystem-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }



    public List<Interviews> findAllInterviewses(String empNo) {
        List<Interviews> list = null;
        try {
            Query query = em.createNamedQuery("Interviews.findByEmpNo");
            query.setParameter("empNo", empNo);
            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }
    public List<Interviews> findAllbyApplicant() {
        List<Interviews> list = null;
        try {
            Query query = em.createNamedQuery("Interviews.findByApplicants");
            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }
    
    public List<Interviews> filterAllInterviewses(String empNo,Date today) {
        List<Interviews> list = null;
        try {
            Query query = em.createNamedQuery("Interviews.findAllInter");
            query.setParameter("empNo", empNo);
            query.setParameter("today", today);
            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }

    public void deleteInterviews(String id) {
        Query q = em.createNamedQuery("Interviews.findStatusByAppID");
        q.setParameter("applicantID", id);
        int a = q.executeUpdate();

    }

    public int countInterviews(String empNo) {
        int i = 0;
        try {
            i = ((Long) em.createNamedQuery("Interviews.countByEmpNo").setParameter("empNo", empNo).getSingleResult()).intValue();
        } catch (NoResultException e) {
            return 0;
        }
        return i;
    }
//
//    public int totalInterviews(String id) {
//        int i = 0;
//        try {
//            i = ((Long) em.createNamedQuery(" Interviews.totalInterviews").setParameter("applicant", id).getSingleResult()).intValue();
//        } catch (NoResultException e) {
//            return 0;
//        }
//        return i;
//    }

    public int countStatusInterviews(int status) {
        int i = 0;
        try {
            i = ((Long) em.createNamedQuery("Interviews.countStatus").setParameter("status", status).getSingleResult()).intValue();
        } catch (NoResultException e) {
            return 0;
        }
        return i;
    }

    public int countByVacancyID() {
        int i = 0;
        try {
            i = ((Long) em.createNamedQuery("Interviews.countByVacancyID").getSingleResult()).intValue();
        } catch (NoResultException e) {
            return 0;
        }
        return i;
    }

//    public int countInterviews(String empNo) { Interviews.countByVacancyID
//        int i = 0;
//        try {
//               Query query = em.createNamedQuery("Interviews.countByEmpNo");
//               query.setParameter("empNo", empNo);
//               i=query.getMaxResults();
//        } catch (NoResultException e) {
//            return 0;
//        }
//        return 5;
//    }
    public void updateInterViews(List<Interviews> List) {
        for (Interviews i : List) {
            em.merge(i);
        }
    }

    public void updateOneInterViews(Interviews inter) {
        em.merge(inter);
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public Interviews addNewInterview(Interviews interview) {
        try {
            em.persist(interview);
            return interview;
        } catch (Exception e) {
            return null;
        }
    }

    public List<Interviews> findByStatus(int status) {
        List<Interviews> list = null;
        try {
            Query query = em.createNamedQuery("Interviews.findStatus");
            query.setParameter("status", status);
            list = query.getResultList();
        } catch (NoResultException e) {
            return null;
        }
        return list;
    }

    public List<Interviews> findByAppIDAndVaID(String applicantID) {
        List<Interviews> lstInter = null;
        try {
            Query query = em.createNamedQuery("Interviews.findByApplicantID");
            query.setParameter("applicantID", applicantID);
            lstInter = query.getResultList();
            return lstInter;
        } catch (NoResultException e) {
            return null;
        }
    }

    public int totalVacancy(int Status, String id) {
        int i = 0;
        try {
            i = ((Long) em.createNamedQuery("Interviews.countByIdVacancy").setParameter("status", Status).setParameter("vacancyID", id).getSingleResult()).intValue();
        } catch (NoResultException e) {
            return 0;
        }
        return i;
    }

    public List<Interviews> findIdVacancyInterviews(String id) {
        List<Interviews> lstInter = null;
        try {
            Query query = em.createNamedQuery("Interviews.findIdVacancyInterviews");
            query.setParameter("vacancyID", id);
            lstInter = query.getResultList();
            return lstInter;
        } catch (NoResultException e) {
            return null;
        }
    }

    public List<Interviews> findAllreportInterviews() {
        List<Interviews> lstInter = null;
        try {
            Query query = em.createNamedQuery("Interviews.findAll");
            lstInter = query.getResultList();
            return lstInter;
        } catch (NoResultException e) {
            return null;
        }
    }
    
}
